package com.cnswhy.cloud.server.merchant.controller.merchant;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.server.core.dto.MerchantPageDTO;
import com.cnswhy.cloud.server.core.dto.MerchantSaveDTO;
import com.cnswhy.cloud.server.core.dto.MerchantUpdateDTO;
import com.cnswhy.cloud.server.core.entity.Merchant;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import com.cnswhy.cloud.server.merchant.dto.MerchantVerifyUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商户基本信息
 * </p>
 *
 * @author cnswhy
 * @date 2020-11-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/merchant")
@Api(value = "Merchant", tags = "商户基本信息")
//@PreAuth(replace = "merchant:")
public class MerchantController extends SuperController<MerchantService, Long, Merchant, MerchantPageDTO, MerchantSaveDTO, MerchantUpdateDTO> {
    @ApiOperation(value = "审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids[]", value = "主键id", dataType = "array", paramType = "query"),
            @ApiImplicitParam(name = "verifyStatus", value = "1审核通过 3审核拒绝", dataType = "long", paramType = "query"),
    })
    @PostMapping("/verifyStatus")
    @Transactional
    public R<Boolean> verifyStatus(@RequestBody MerchantVerifyUpdateDTO merchantVerifyUpdateDTO) {
        log.info(JSON.toJSONString(merchantVerifyUpdateDTO));
        List<Merchant> merchantList = baseService.listByIds(merchantVerifyUpdateDTO.getIds());
        for(var var : merchantList){
            if(var.getStatus() != 2){
                return fail("存在已审核记录，请检查申请记录");
            }
            var.setStatus(merchantVerifyUpdateDTO.getVerifyStatus());
            baseService.updateById(var);
        }
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Merchant> merchantList = list.stream().map((map) -> {
            Merchant merchant = Merchant.builder().build();
            return merchant;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(merchantList));
    }
}
